package com.wanmait.movies.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.wanmait.movies.util.DBHelper;
import com.wanmait.movies.vo.Actor;
import com.wanmait.movies.vo.Movie;

public class MovieActorDao {
	private MovieActorDao() {};
	private static MovieActorDao a=new MovieActorDao();
	public static MovieActorDao getMovieActorDao() {
		return a;
	}
	//删除
	
		public void deleteActor(int id) {
			Connection co = DBHelper.getDBHelper().getconn();
			PreparedStatement ps = null;
			ResultSet rs = null;
			
			String sql ="delete from movie_actor  where movie_id=?";
			try {
				
				ps=co.prepareStatement(sql);
				ps.setInt(1, id);
				ps.executeUpdate();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally {
			DBHelper.getDBHelper().close(rs, ps, co);
			}
		}
	
		//删除
		
			public void deleteActorMovie(int id) {
				Connection co = DBHelper.getDBHelper().getconn();
				PreparedStatement ps = null;
				ResultSet rs = null;
				
				String sql ="delete from movie_actor  where actor_id=?";
				try {
					
					ps=co.prepareStatement(sql);
					ps.setInt(1, id);
					ps.executeUpdate();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				finally {
				DBHelper.getDBHelper().close(rs, ps, co);
				}
			}
	//���ݵ�ӰID��ȡ��Ա����
	public ArrayList<Actor> movieActorGetMovieId(int movieId) {
		ArrayList<Actor> allActor=new ArrayList<>();
		DBHelper a=DBHelper.getDBHelper();
		Connection co = a.getconn();
		String sql="select * from movie_actor,actor where actor_id=actor.id and movie_id=?";
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = co.prepareStatement(sql);
			ps.setInt(1, movieId);
			rs=ps.executeQuery();
			while(rs.next()){
				Actor actor=new Actor();
				actor.setActorName(rs.getString("actor_name"));
				actor.setActorImage(rs.getString("actor_image"));
				actor.setId(rs.getInt("actor.id"));
				allActor.add(actor);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			a.close(rs, ps, co);
			return allActor;
		}
	}
	
	//添加一部电影所对应的所有演员
		public void addMovieActor(String ids,int movieId) {
			DBHelper a=DBHelper.getDBHelper();
			Connection co = a.getconn();
			
			PreparedStatement ps = null;
			ResultSet rs = null;
			
			StringBuilder sb =new StringBuilder();
			sb.append("  INSERT INTO movie_actor(movie_id,actor_id)   ");
			sb.append("  SELECT movie.id,actor.id FROM movie,actor WHERE movie.id=?  ");
			sb.append("  and actor.id IN ("+ids+")");
			
			String sql = sb.toString();
			
			try {
				ps=co.prepareStatement(sql);
				ps.setInt(1, movieId);
				
				ps.executeUpdate();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally {
				a.close(rs, ps, co);
			}
					
		}
	
	
	
	
	
	
}
